﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace WaveformFigure
{
    /// <summary>
    /// 模拟波形图采样
    /// 数据源模拟使用timer控件模拟
    /// </summary>
    public partial class MainForm : Form
    {
        #region 属性
        /// <summary>
        /// 随机数
        /// </summary>
        private Random RandomNum = new Random();
        #endregion

        #region 构造函数
        public MainForm()
        {
            InitializeComponent();
            //价格chart控件里面的series设置为扫描线，最接近波形图
            chart1.Series["Series1"].ChartType = SeriesChartType.FastLine;
            //波形图为绿色
            chart1.Series["Series1"].Color = Color.Green;
            chart1.Series["Series1"].XValueType = ChartValueType.String;
        }
        #endregion

        #region 事件
        private void timer1_Tick(object sender, EventArgs e)
        {
            //其他的都是为了模拟数据源，这个方法体代码才是要看的地方
            //控制扫描线图X轴的密度，如果不减少则会导致，X轴太密集了
            if (chart1.Series["Series1"].Points.Count > 10)
            {
                chart1.Series["Series1"].Points.RemoveAt(0);

            }
            //AddY就是每次采集来的数据，这里采用了随机数模拟
            //chart1.Series["Series1"].Points.AddY(RandomNum.Next(10, 100));
            chart1.Series["Series1"].Points.AddXY(DateTime.Now.ToString("HH:mm:ss"), RandomNum.Next(10, 100));
            //刷新chart控件
            chart1.Invalidate();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Interval = 1000;
        }
        #endregion
    }
}
